package com.sleepcamel.resteasyConfigurator.components.tree.strategies;

import java.util.ArrayList;
import java.util.List;

import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.rest.domain.Project;
import com.sleepcamel.rest.domain.ProjectPackage;
import com.sleepcamel.rest.domain.TreeNode;
import com.sleepcamel.resteasyConfigurator.components.tree.ITreeContentStrategy;

public class PublishTreeContentStrategy implements ITreeContentStrategy {

	@Override
	public Object[] getChildren(Object arg0) {
		if (arg0 instanceof Project) {
			Project project = (Project) arg0;
			List<Mapping> projectMappings = new ArrayList<Mapping>();
			
			List<ProjectPackage> projectPackages = project.getChilds();
			for(ProjectPackage projectPackage:projectPackages){
				projectMappings.addAll(projectPackage.getAllPackageMappings());
			}
			return projectMappings.toArray();
		}
		if (arg0 instanceof TreeNode) {
			return ((TreeNode) arg0).getChilds().toArray();
		}
		return null;
	}

	@Override
	public Object getParent(Object arg0) {
		if (arg0 instanceof Mapping) {
			Mapping mapping = (Mapping) arg0;
			ProjectPackage projectPackage = mapping.getParent();
			while(projectPackage instanceof ProjectPackage){
				projectPackage = projectPackage.getParent();
			}
			return projectPackage;
		}
		if (arg0 instanceof TreeNode) {
			return ((TreeNode) arg0).getParent();
		}
		return null;
	}

}
